package com.cloudbroker.bsc.common.search.common;

import com.alibaba.fastjson.JSON;
import com.cloudbroker.bcs.common.configure.ConfigUtil;
import com.cloudbroker.bsc.common.search.core.SearchEngineFactory;
import com.cloudbroker.bsc.common.search.elasticsearch.ElasticsearchEntity;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 搜所引擎
 *
 * @create 2016-6-11
 * @author moyin18602
 * @version
 */
public class SearchEngineAdapter extends SearchEngineFactory {
    public static String DATABASE_NAME;
    public static String[] DATABASE_NAMES;

    SearchEngineAdapter() {
        // 获取对应的配置集
        DATABASE_NAME = ConfigUtil.get("searchEngine.elasticsearch.db","vipstu");
        DATABASE_NAMES = new String[1];
        DATABASE_NAMES[0] = DATABASE_NAME;
    }

    /**
     * 新增
     * @param table_name
     * @param row_id
     * @param obj
     * @param <T>
     * @return
     */
    public <T> boolean save(String table_name, String row_id, T obj){
        return getSearchEngine().save(DATABASE_NAME,table_name,row_id,obj);
    }

    /**
     *
     * @param list
     * @param <T>
     * @return
     */
    public <T> boolean save(List<ElasticsearchEntity> list){
        for (ElasticsearchEntity entity: list) {
            if(StringUtils.isEmpty(entity.getDatabase_name())){
                entity.setDatabase_name(DATABASE_NAME);
            }
        }
        return getSearchEngine().save(list);
    }

    /**
     * 修改数据
     * @param table_name	表名
     * @param row_id		主键id
     * @param obj			可以是任意对象
     * @param <T>
     * @return
     */
    public <T> boolean update(String table_name, String row_id, T obj) {
        return getSearchEngine().update(DATABASE_NAME,table_name,row_id,obj);
    }

    public <T> boolean update_query(Map<String,String> params_map, T obj) {
        List<Map> list = query(params_map);
        for (Map m:list) {
            update(m.get("table_name").toString(),m.get("row_id").toString(),obj);
        }
        return true;
    }

    /**
     * 删除数据
     * @param table_name 表名
     * @param row_id	  主键id
     * @return
     */
    public boolean delete(String table_name, String row_id) {
        return getSearchEngine().delete(DATABASE_NAME,table_name,row_id);
    }

    public boolean delete_query(Map<String,String> nap) {
        List<Map> list = query(nap);
        for (Map m:list) {
            delete(m.get("table_name").toString(),m.get("row_id").toString());
        }
        return true;
    }

    /**
     * 数据检索
     * @param map  key 数据库字段  value 查询的值
     *             可支持多个key组合查询
     * @param start
     * @param limit
     * @return
     */
    public List<Map> query(Map<String,String> map ,Integer start, Integer limit) {
        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setQueryBuilder(SearchEngineUtil.mapToQueryBuilder(map));
        elasticsearchEntity.setStart(start);
        elasticsearchEntity.setLimit(limit);
        elasticsearchEntity.setExplain(true);
        return getSearchEngine().query(elasticsearchEntity);
    }


    /**
     * 数据检索
     * @param map  key 数据库字段  value 查询的值
     *             可支持多个key组合查询
     * @return
     */
    public List<Map> query(Map<String,String> map) {
        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setQueryBuilder(SearchEngineUtil.mapToQueryBuilder(map));
        elasticsearchEntity.setExplain(true);
        elasticsearchEntity.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
        return getSearchEngine().query(elasticsearchEntity);
    }

    /**
     * 数据检索
     * @param table_name  表名
     * @param map  key 数据库字段  value 查询的值
     *             可支持多个key组合查询
     * @return
     */
    public List<Map> query(String table_name, Map<String,String> map) {
        String [] table_names = {table_name};
        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setTable_names(table_names);
        elasticsearchEntity.setQueryBuilder(SearchEngineUtil.mapToQueryBuilder(map));
        elasticsearchEntity.setExplain(true);
        return getSearchEngine().query(elasticsearchEntity);
    }

    /**
     * 数据检索
     * @param table_name  表名
     * @param map  key 数据库字段  value 查询的值
     *             可支持多个key组合查询
     * @param start
     * @param limit
     * @return
     */
    public List<Map> query(String table_name, Map<String,String> map , Integer start, Integer limit) {
        String [] table_names = {table_name};
        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setTable_names(table_names);
        elasticsearchEntity.setQueryBuilder(SearchEngineUtil.mapToQueryBuilder(map));
        elasticsearchEntity.setStart(start);
        elasticsearchEntity.setLimit(limit);
        elasticsearchEntity.setExplain(true);
        return getSearchEngine().query(elasticsearchEntity);
    }


    /**
     * 数据检索
     * @param table_name  表名
     * @param map  key 数据库字段  value 查询的值
     *             可支持多个key组合查询
     * @param start 分页开始值
     * @param limit 分页取值数量
     * @param field_sort 排序值
     * @param sort_order 排序顺序 
     * @return
     */
    public List<Map> query(String table_name, Map<String,String> map , Integer start, Integer limit, String field_sort, String sort_order) {
        String [] table_names = {table_name};
        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setTable_names(table_names);
        elasticsearchEntity.setQueryBuilder(SearchEngineUtil.mapToQueryBuilder(map));
        elasticsearchEntity.setStart(start);
        elasticsearchEntity.setLimit(limit);
        elasticsearchEntity.setExplain(true);
        elasticsearchEntity.setField_sort(field_sort);
        elasticsearchEntity.setSort_order(sort_order);
        return getSearchEngine().query(elasticsearchEntity);
       
 
    }
    

    /**
     * 数据检索
     * @param table_name  表名
     * @param map  key 数据库字段  value 查询的值
     *             可支持多个key组合查询
     * @param start
     * @param limit
     * @return
     */
    public List<Map> query(String[] table_names, Map<String,String> map , Integer start, Integer limit,String field_sort, String sort_order) {
        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setTable_names(table_names);
        elasticsearchEntity.setQueryBuilder(SearchEngineUtil.mapToQueryBuilder(map));
        elasticsearchEntity.setStart(start);
        elasticsearchEntity.setLimit(limit);
        elasticsearchEntity.setExplain(true);
        elasticsearchEntity.setField_sort(field_sort);
        elasticsearchEntity.setSort_order(sort_order);
//        elasticsearchEntity.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
        return getSearchEngine().query(elasticsearchEntity);
        
    }

    public static void main(String[] args){
        SearchEngineAdapter SearchEngineAdapter = new SearchEngineAdapter();

        String [] table_names = {"test_type"};
        BoolQueryBuilder bqb = QueryBuilders.boolQuery();

        bqb.must(QueryBuilders.rangeQuery("amount").gt("1").lt("9223372036854775807990"));

        ElasticsearchEntity elasticsearchEntity = new ElasticsearchEntity();
        elasticsearchEntity.setDatabase_names(DATABASE_NAMES);
        elasticsearchEntity.setTable_names(table_names);
        elasticsearchEntity.setQueryBuilder(bqb);
        elasticsearchEntity.setExplain(true);
        elasticsearchEntity.setField_sort("amount");
        elasticsearchEntity.setSort_order("desc");
        List<Map> l = SearchEngineAdapter.getSearchEngine().query(elasticsearchEntity);
        System.out.println(JSON.toJSON(l));
    }

	public Object deleteAll() {
		Object s = getSearchEngine().deleteAll();
		return s;
	}
}
